package vn.personal.anhht.bloback12.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class CallBlockDatabaseCallsBlockAdapter {
    public static final String KEY_ID = "id";
    public static final String KEY_NAME = "fullname";
    public static final String KEY_PHONE = "phone";
    public static final String KEY_TIME = "time";
    public static final String KEY_TYPE = "type";
    
    public static final String TABLE_CALLSBLOCK = "callsblock";
    
    private Context context;
    private SQLiteDatabase database;
    private CallBlockDatabaseHelper dbHelper;
    
    
    public CallBlockDatabaseCallsBlockAdapter(){
        this.context = null;
        dbHelper = new CallBlockDatabaseHelper(context);
    }
    
    public CallBlockDatabaseCallsBlockAdapter(Context _context){
        this.context = _context;
        dbHelper = new CallBlockDatabaseHelper(context);
    }
    
    public CallBlockDatabaseCallsBlockAdapter open() throws SQLException {
        database = dbHelper.getWritableDatabase();
        return this;
    }
    
    public void close(){
        dbHelper.close();
    }

    public android.database.Cursor selectAllCallsBlock(){
        String[] columns = {KEY_ID, KEY_NAME, KEY_PHONE, KEY_TIME, KEY_TYPE};
        String selection = null;
        String[] selectionAgrs = null;
        String groupBy = null;
        String having = null;
        String orderBy = KEY_TIME + " DESC";
        
        return this.database.query(TABLE_CALLSBLOCK, columns, selection, selectionAgrs, groupBy, having, orderBy);
    }

    public android.database.Cursor selectCallsBlockById(int _id){
        String[] columns = {KEY_ID, KEY_NAME, KEY_PHONE, KEY_TIME, KEY_TYPE};
        String selection = KEY_ID + " = " + _id;
        String[] selectionAgrs = null;
        String groupBy = null;
        String having = null;
        String orderBy = null;
        
        return this.database.query(TABLE_CALLSBLOCK, columns, selection, selectionAgrs, groupBy, having, orderBy);
    }
    
    public android.database.Cursor selectCallsBlockByPhone(String phone){
        String[] columns = {KEY_ID, KEY_NAME, KEY_PHONE, KEY_TIME, KEY_TYPE};
        String selection = KEY_PHONE + " = '" + phone + "'";
        String[] selectionAgrs = null;
        String groupBy = null;
        String having = null;
        String orderBy = null;
        return this.database.query(TABLE_CALLSBLOCK, columns, selection, selectionAgrs, groupBy, having, orderBy);
    }
    
    private ContentValues initParameters(
            String phone, 
            String time, 
            String name, 
            int type){
        ContentValues parameters = new ContentValues(); 
        parameters.put(KEY_PHONE, phone);
        parameters.put(KEY_TIME, time);
        parameters.put(KEY_NAME, name);
        parameters.put(KEY_TYPE, type);
        return parameters;  
    }

    public long addCallsBlock(
            String phone, 
            String time, 
            String name, 
            int type){
        
        ContentValues parameters = initParameters(phone, time, name, type);
        return this.database.insert(TABLE_CALLSBLOCK, null, parameters);
    }

    public boolean updateCallsBlock(
            int id,
            String phone, 
            String time, 
            String name, 
            int type){
        ContentValues parameters = initParameters(phone, time, name, type);
        return this.database.update(TABLE_CALLSBLOCK, parameters, KEY_ID + " = " + id, null) > 0;
    }
    
    public boolean updateAdvanced(ContentValues parameters, String whereClause, String[] whereArgs){
        
        return this.database.update(TABLE_CALLSBLOCK, parameters, whereClause, whereArgs) > 0;
    }

    public boolean deleteCallsBlock(int _id){
        return this.database.delete(TABLE_CALLSBLOCK, KEY_ID + " = " + _id, null) > 0;
    }
    
    public boolean deleteAll(){
        return this.database.delete(TABLE_CALLSBLOCK, null , null) > 0;
    }
}
